package gatperdut.chato.core.interpreter.mainmenu;

import gatperdut.chato.constants.CSConstantsCommandResult.CommandExecutionResult;
import gatperdut.chato.constants.CSConstantsString;
import gatperdut.chato.core.ChatoServer;
import gatperdut.chato.core.entities.PlayerController;
import gatperdut.chato.core.interpreter.PlayerInterpreter;

public class PlayerMainMenu {

    private final PlayerInterpreter interpreter;

    public PlayerMainMenu( PlayerInterpreter interpreter ) {
        this.interpreter = interpreter;
    }

    public CommandExecutionResult handleMainMenu( String command ) {
        if ( "1".equals( command ) ) {
            if ( getController().getPlayer().getCurrentPC() != null ) {
                return CommandExecutionResult.MAIN_MENU_ENTER_GAME;
            }
            else {
                getController().tx( "Unfortunately you don't seem to have an available characters..." );
                return CommandExecutionResult.OK;
            }
        }
        else if ( "2".equals( command ) ) {
            getController().tx( "Bye " + getController().getPlayer().getUsername() + "! Come back soon!" );
            return CommandExecutionResult.QUIT_LOGOUT;
        }
        else {
            getController().tx( "That's not a valid option, my friend." );
            return CommandExecutionResult.OK;
        }
    }

    public String getMainMenuString() {
        StringBuffer sb = new StringBuffer();

        if ( getController().getPlayer().getCurrentPC() != null ) {
            sb.append( "  1) start playing (" + getController().getPlayer().getCurrentPC().getName() + ")" );
        }
        else {
            sb.append( "  x) no character available" );
        }
        sb.append( CSConstantsString.NEWLINE );
        sb.append( "  2) exit" );

        return sb.toString();
    }

    public PlayerController getController() {
        return getInterpreter().getController();
    }

    public ChatoServer getChatoServer() {
        return getInterpreter().getController().getChatoServer();
    }

    public PlayerInterpreter getInterpreter() {
        return interpreter;
    }
}
